/**
 * class  Label
 * 文本组件
 * author haicent
 * email haicent@126.com
 * version 0.01
 */
package as3.component.controls
{
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	
	import as3.component.Component;
	import as3.component.event.SizeChangeEvent;
	import as3.component.geom.IntRectangle;

	[Event(name="sizeChange", type="as3.component.event.SizeChangeEvent")]
	public class Label extends Component
	{
		
		protected var _textField:TextField;
		private var _text:String = "";
		
		private var useHtmlText:Boolean = false;
		private var textChange:Boolean = false;
		protected var _adaptive:Boolean = true;
		private var _textFormatChange:Boolean = false;
		
		protected var defaultWidth:Number = 80;
		protected var defaultHeight:Number = 16;
		
		private var _selectable:Boolean;
		private var _wordWrap:Boolean;
		private var _multiline:Boolean;
		
		protected var _offWidth:Number = 8;
		protected var _offHeight:Number = 8;
		
		private var _defaultFormat:TextFormat;
		
		private var _color:uint = 0xffffff;
		private var _fontSize:uint = 12;
		private var _bold:Boolean = false;
		private var _italic:Boolean = false;
		private var _underLine:Boolean = false;
		private var _fontName:String = "宋体";
		private var _algin:String = TextFormatAlign.LEFT;
		private var _leading:Number = 0;

		
		public function Label()
		{
			super();
			mouseEnabled = false;
		}
		
		override protected function initCom():void
		{
			setMasked(true);
			_defaultFormat = new TextFormat(_fontName, _fontSize, _color, _bold, _italic, _underLine, null, null, TextFormatAlign.CENTER, null, null, true, _leading);
			_defaultFormat.align = algin;
			_width = defaultWidth;
			_height = defaultHeight;
			_textField = new TextField();
			_textField.width = defaultWidth;
			_textField.height = defaultHeight;
			_selectable = _textField.selectable;
			_wordWrap = _textField.wordWrap;
			_textField.defaultTextFormat = _defaultFormat;
			addChild(_textField);
		}
		
		override protected function initListener():void
		{
			super.initListener();
		}
		
		/**
		 * 设置文本颜色 
		 * @param value
		 * 
		 */		
		public function set color(value:uint):void
		{
			if (_color == value) return;
			_color = value;
			_defaultFormat.color = color;
			_textField.defaultTextFormat = _defaultFormat;
			_textField.text = _textField.text;
		}
		
		public function get color():uint
		{
			return _color;
		}
		
		/**
		 *设置文本大小 
		 * @param value
		 * 
		 */		
		public function set fontSize(value:uint):void
		{
			if (_fontSize == value) return;
			_defaultFormat.size = value;
			_fontSize = value;
			_textFormatChange = true;
			propertyChanged = true;
		}
		
		public function get fontSize():uint
		{
			return _fontSize;
		}
		
		/**
		 *设置文本粗体 
		 * @param value
		 * 
		 */		
		public function set bold(value:Boolean):void
		{
			if (_bold == value) return;
			_defaultFormat.bold = value;
			_textField.defaultTextFormat = _defaultFormat;
			_bold = value;
			_textFormatChange = true;
			propertyChanged = true;
		}
		
		public function get bold():Boolean
		{
			return _bold;
		}
		
		/**
		 *设置文本斜体 
		 * @param value
		 * 
		 */		
		public function set italic(value:Boolean):void
		{
			if (_italic == value) return;
			_italic = value;
			_textFormatChange = true;
			propertyChanged = true;
		}
		
		public function get italic():Boolean
		{
			return _italic;
		}
		
		/**
		 *设置下划线 
		 * @param value
		 * 
		 */		
 		public function set underLine(value:Boolean):void
		{
			if (_underLine == value) return;
			_underLine = value;
			_textFormatChange = true;
			propertyChanged = true;
		}
		
		public function get underLine():Boolean
		{
			return _underLine;
		}
		
		public function set fontName(value:String):void
		{
			if (value == _fontName) return;
			_fontName = value;
			_textFormatChange = true;
			propertyChanged = true;
		}
		
		public function get fontName():String
		{
			return _fontName;
		}
		
		public function set algin(value:String):void
		{
			if (_algin == value) return;
			
			if (value == TextFormatAlign.CENTER
				|| value == TextFormatAlign.LEFT
				|| value == TextFormatAlign.RIGHT
				|| value == TextFormatAlign.JUSTIFY) _algin = value;
			_defaultFormat.align = algin;
			_textField.defaultTextFormat = _defaultFormat;
			_textFormatChange = true;
			propertyChanged = true;
		}
		
		public function get algin():String
		{
			return _algin;
		}
		
		public function set leading(value:Number):void
		{
			if (_leading == value) return;
			_leading = value;
			_defaultFormat.leading = value;
			_textField.defaultTextFormat = _defaultFormat;
			_textFormatChange = true;
			propertyChanged = true;
		}
		
		public function get leading():Number
		{
			return _leading;
		}
		
		override public function get width():Number
		{
			return _width;
		}
		
		override public function set width(value:Number):void
		{
			_width = value;
			_adaptive = false;
			reSize();
		}
		
		override public function get height():Number
		{
			return _height;
		}
		
		override public function set height(value:Number):void
		{
			_height = value;
			_adaptive = false;
			reSize();
		}
		
		public function set text(text:String):void
		{
			if(!text)
				text = '';
			if (_text == text && _text != "") return;
			
			_text = text;
			textChange = true;
			useHtmlText = false;
			_textField.defaultTextFormat = _defaultFormat;
			_textField.htmlText = _text;
			reSize();
		}
		
		public function get text():String
		{
			return _textField.text;
		}
		
		public function set htmlText(value:String):void
		{
			if (_text == value) return;
			
			_text = value;
			useHtmlText = true;
			_textField.htmlText = _text;
			_textField.defaultTextFormat = _defaultFormat;
			reSize();
		}
		
		public function get htmlText():String
		{
			return _textField.htmlText;
		}
		
		public function get textField():TextField
		{
			return _textField;
		}
		
		public function get selectable():Boolean
		{
			return _selectable;
		}
		
		public function set selectable(value:Boolean):void
		{
			if (_selectable == value) return;
			
			_selectable = value;
			propertyChanged = true;
		}
		
		public function get wordWrap():Boolean
		{
			return _wordWrap;
		}
		
		public function set wordWrap(value:Boolean):void
		{
			if (_wordWrap == value) return;
			
			_wordWrap = value;
			if (_textField.wordWrap != _wordWrap) _textField.wordWrap = _wordWrap;
			reSize();
		}
		
		public function set multiline(value:Boolean):void
		{
			if (_multiline == value) return;
			
			_multiline = value;
			if (_textField.multiline != _multiline) _textField.multiline = _multiline;
			reSize();
		}
		
		public function set adaptive(value:Boolean):void
		{
			if (_adaptive == value) return;
			
			_adaptive = value;
			reSize();
		}
		
		public function get adaptive():Boolean
		{
			return _adaptive;
		}
		
		public function getTextField():TextField
		{
			return _textField;
		}
		
		protected function reSize():void
		{
			if (_adaptive)
			{								
//				_width = _textField.textWidth + _offWidth;
//				_height = _textField.textHeight + _offHeight;
//				_textField.width = _width;
//				_textField.height = _height;
				_textField.autoSize = TextFieldAutoSize.LEFT;
				_width = _textField.width;
				_height = _textField.height;
				super.width = _width;
				super.height = _height;				
			}
			else
			{
				_textField.autoSize = TextFieldAutoSize.NONE;
				_textField.width = _width;
				_textField.height = _height;
			}
			setMaskRect(new IntRectangle(0, 0, _width, _height));
			dispatchEvent(new SizeChangeEvent(SizeChangeEvent.SIZE_CHANGE));
		}
		
		/**
		 *修改基本属性后，更新显示 
		 * 
		 */		
		override public function updateDisplay():void
		{
			super.updateDisplay();
			
			if (_textField.selectable != _selectable) _textField.selectable = _selectable;
			
			var sizeChange:Boolean = false;
			if (_textFormatChange)
			{
				_defaultFormat.underline = _underLine;
				if (_defaultFormat.size != fontSize)
				{
					_defaultFormat.size = fontSize;
					sizeChange = true;
				}
				
				if (_defaultFormat.bold != bold)
				{
					_defaultFormat.bold = bold;
					sizeChange = true;
				}
				
				if (_defaultFormat.font != fontName)
				{
					_defaultFormat.font = fontName;
					sizeChange = true;
				}
				
				if (_defaultFormat.italic != _italic)
				{
					_defaultFormat.italic = _italic;
					sizeChange = true;
				}
				if (_defaultFormat.align != _algin)
				{
					_defaultFormat.align = _algin;
				}
				if (_defaultFormat.leading != _leading)
				{
					_defaultFormat.kerning = true;
					_defaultFormat.leading = _leading;
					sizeChange = true;
				}
				_textField.defaultTextFormat = _defaultFormat;
				if (useHtmlText) {
					_textField.htmlText = _textField.htmlText;
				} else {
					_textField.text = _textField.text;
				}
				_textFormatChange = false;
				if (sizeChange)
				{
					reSize();
				}
			}
		}
		
	}
}